package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdClientMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.WmWarehouseMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdClient;
import com.jxdinfo.hussar.example.common.schedulingtask.model.WmWarehouse;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdClientService;
import com.jxdinfo.hussar.example.common.schedulingtask.service.WmWarehouseService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdClientVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.WmWarehouseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-08
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.WmWarehouseServiceImpl")
public class WmWarehouseServiceImpl extends ServiceImpl<WmWarehouseMapper, WmWarehouse> implements WmWarehouseService {

    private static final Logger logger = LoggerFactory.getLogger(WmWarehouseServiceImpl.class);


    @Autowired
    private WmWarehouseService wmWarehouseService;

    @Autowired
    private WmWarehouseMapper wmWarehouseMapper;

    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-仓库表-开始同步仓库表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的仓库表信息
        List<WmWarehouseVO> allWmWareHouse = wmWarehouseMapper.selectAllWmWarehouse();
        // 2仓库表信息转换成主键-对象map集合
        Map<Long, WmWarehouseVO> allWmWarehousesMap = allWmWareHouse.stream().collect(Collectors.toMap(WmWarehouseVO::getWarehouseId, t -> t));
        // 3仓库表信息中的id转换成id列表
        List<Long> erpAllWmWarehouseIdList = allWmWareHouse.stream().map(WmWarehouseVO::getWarehouseId).collect(Collectors.toList());
        // 4查询所有的mes仓库表信息
        QueryWrapper<WmWarehouse> wmWarehouseWrapper = new QueryWrapper<>();
        wmWarehouseWrapper.eq("deleted_flag","0");
        List<WmWarehouse> mesWmWarehouses = wmWarehouseMapper.selectList(wmWarehouseWrapper);
        // 4将mes中的仓库表信息转换成主键-对象map集合
        Map<Long, WmWarehouse> allMesWmWarehousesMap = mesWmWarehouses.stream().collect(Collectors.toMap(WmWarehouse::getWarehouseId, t -> t));
        // 5mes中的客户信息转换成id列表,
        List<Long> mesIdList = mesWmWarehouses.stream().map(WmWarehouse::getWarehouseId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllWmWarehouseIdList.size(); i++) {
            if (!mesIdList.contains(erpAllWmWarehouseIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllWmWarehouseIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<WmWarehouse> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                WmWarehouseVO wmWarehouseVO = allWmWarehousesMap.get(insertId);
                WmWarehouse wmWarehouse = new WmWarehouse();
                wmWarehouse.setWarehouseId(wmWarehouseVO.getWarehouseId());
                wmWarehouse.setWarehouseCode(wmWarehouseVO.getWarehouseCode());     // 编码
                wmWarehouse.setWarehouseName(wmWarehouseVO.getWarehouseName());     // 名称
                wmWarehouse.setEnableFlag(wmWarehouseVO.getEnableFlag());     // 是否启用
                wmWarehouse.setScheduleTime(nowTime);
                wmWarehouse.setCreateBy(wmWarehouseVO.getCreateBy());
                wmWarehouse.setCreateTime(wmWarehouseVO.getCreateTime());
                wmWarehouse.setUpdateBy(wmWarehouseVO.getUpdateBy());
                wmWarehouse.setUpdateTime(wmWarehouseVO.getUpdateTime());
                wmWarehouse.setErpWarehouseId(wmWarehouseVO.getWarehouseId());  //erp主键
                insertList.add(wmWarehouse);
            }
            wmWarehouseService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<WmWarehouse> wmWarehouseUpdateList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllWmWarehouseIdList.contains(mesIdList.get(i))){
                wmWarehouseUpdateList.add(allMesWmWarehousesMap.get(mesIdList.get(i)));
            }
        }
        if (wmWarehouseUpdateList.size()>0) {
            ArrayList<WmWarehouse> wmWarehousesUpdateList = new ArrayList<>();
            for (WmWarehouse wmWarehouse : wmWarehouseUpdateList) {
                Long erpWmWarehouseId = wmWarehouse.getWarehouseId();
                WmWarehouseVO wmWarehouseVO = allWmWarehousesMap.get(erpWmWarehouseId);
                wmWarehouse.setWarehouseCode(wmWarehouseVO.getWarehouseCode());              // 编码
                wmWarehouse.setWarehouseName(wmWarehouseVO.getWarehouseName());              // 名称
                wmWarehouse.setEnableFlag(wmWarehouseVO.getEnableFlag());              // 生效状态
                wmWarehouse.setScheduleTime(nowTime);
                wmWarehouse.setCreateBy(wmWarehouseVO.getCreateBy());
                wmWarehouse.setCreateTime(wmWarehouseVO.getCreateTime());
                wmWarehouse.setUpdateBy(wmWarehouseVO.getUpdateBy());
                wmWarehouse.setUpdateTime(wmWarehouseVO.getUpdateTime());
                wmWarehousesUpdateList.add(wmWarehouse);
            }
            wmWarehouseService.updateBatchById(wmWarehousesUpdateList);
        }
    }
}
